package com.ihr360.payroll.controller.spreadsheet.validator;

import com.google.common.collect.Lists;
import com.ihr360.commons.context.session.Ihr360Session;
import com.ihr360.commons.context.session.Ihr360SessionContextHolder;
import com.ihr360.commons.lang.DateUtils;
import com.ihr360.excel.specification.ColumnSpecification;
import com.ihr360.excel.specification.CommonSpecification;
import com.ihr360.excel.validation.SpreadSheetValidator;
import com.ihr360.excel.validation.annonation.SpreadSheetValid;
import com.ihr360.excel.vo.CustomSpreadSheetValidateResult;
import com.ihr360.excel.vo.SpreadSheetSpecification;
import com.ihr360.excel.vo.SpreadSheetValidatorRequest;
import com.ihr360.payroll.service.attendancereport.AttendanceMonthlyReportSummaryService;
import com.ihr360.payroll.service.attendancereport.impl.AttendanceMonthlyReportSummaryServiceImpl;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@SpreadSheetValid(code = "dingDingImport")
public class DingDingImportValidator implements SpreadSheetValidator {

    @Autowired
    private AttendanceMonthlyReportSummaryService attendanceMonthlyReportSummaryService;

    @Override
    public CustomSpreadSheetValidateResult customValidate(SpreadSheetValidatorRequest request) {

        Ihr360Session session = Ihr360SessionContextHolder.getSession();
        if (session == null || session.getUserInfo() == null
                || session.getUserInfo().getCompanyId() == null
                || session.getUserInfo().getUserId() == null) {
            ValidationUtils.reject(null, "【钉钉导入校验】请求参数无效");
        }

        Map<String, Object> extraParams = request.getExtraParams();
        if (extraParams == null) {
            ValidationUtils.reject(null, "【钉钉导入校验】请求参数无效");
        }
        Long salaryPlanId = Long.parseLong(extraParams.get(AttendanceMonthlyReportSummaryServiceImpl.SALARY_PLAN_ID).toString());
        Date summaryDate = DateUtils.parse(extraParams.get(AttendanceMonthlyReportSummaryServiceImpl.SUMMARY_DATE).toString());
        Date summaryStartDate = DateUtils.parse(extraParams.get(AttendanceMonthlyReportSummaryServiceImpl.SUMMARY_START_DATE).toString());
        Date summaryEndDate = DateUtils.parse(extraParams.get(AttendanceMonthlyReportSummaryServiceImpl.SUMMARY_END_DATE).toString());
        Date salaryStartDate = DateUtils.parse(extraParams.get(AttendanceMonthlyReportSummaryServiceImpl.SALARY_START_DATE).toString());
        Date salaryEndDate = DateUtils.parse(extraParams.get(AttendanceMonthlyReportSummaryServiceImpl.SALARY_END_DATE).toString());

        ValidationUtils.rejectIfEmpty(salaryPlanId, "", "【钉钉导入校验】薪资方案不可为空");
        ValidationUtils.rejectIfEmpty(summaryDate, "", "【钉钉导入校验】考勤日期不可为空");
        ValidationUtils.rejectIfEmpty(summaryStartDate, "", "【钉钉导入校验】考勤起始日期不可为空");
        ValidationUtils.rejectIfEmpty(summaryEndDate, "", "【钉钉导入校验】考勤结束日期不可为空");
        ValidationUtils.rejectIfEmpty(salaryStartDate, "", "【钉钉导入校验】薪资起始日期不可为空");
        ValidationUtils.rejectIfEmpty(salaryEndDate, "", "【钉钉导入校验】薪资结束日期不可为空");

        return attendanceMonthlyReportSummaryService.validateExcelData(
                session.getUserInfo().getCompanyId(),
                session.getUserInfo().getUserId(),
                salaryPlanId,
                summaryDate,
                summaryStartDate,
                summaryEndDate,
                salaryStartDate,
                salaryEndDate,
                request);
    }

    @Override
    public SpreadSheetSpecification commonSpecification(SpreadSheetValidatorRequest request) {
        SpreadSheetSpecification spreadSheetSpecification = new SpreadSheetSpecification();
        CommonSpecification commonSpecification = CommonSpecification.createCommonSpecification(true);
        List<List<String>> requiredHeaders = Lists.newArrayList();
        ArrayList<String> staffNameColumn = Lists.newArrayList("姓名");
        requiredHeaders.add(staffNameColumn);
        commonSpecification.setHeaderColumnJudge(requiredHeaders);
        commonSpecification.setStaffNameAlias(staffNameColumn);

        commonSpecification.setTemplateHeaderRowNum(3);
        commonSpecification.setTemplateDataBeginRowNum(4);
        //commonSpecification.setTemplateHeaders();
        spreadSheetSpecification.setCommonSpecification(commonSpecification);

        //必填项
        List<ColumnSpecification> columnSpecifications = new ArrayList<>();
        ColumnSpecification strNullAbleColumnSpe = ColumnSpecification.createCellSpecification(String.class, false);
        strNullAbleColumnSpe.addColumns("姓名");
        ColumnSpecification doubleNullAbleColumnSpe = ColumnSpecification.createCellSpecification(Double.class, false);
        doubleNullAbleColumnSpe.addColumns("法定计薪天数");

        ColumnSpecification doubleColumnSpe = ColumnSpecification.createCellSpecification(Double.class, true);
        doubleColumnSpe.addColumns("月计薪天数");
        doubleColumnSpe.addColumns("应出勤天数");
        doubleColumnSpe.addColumns("实际出勤天数");
        doubleColumnSpe.addColumns("工作日加班");
        doubleColumnSpe.addColumns("休息日加班");
        doubleColumnSpe.addColumns("节假日加班");
        doubleColumnSpe.addColumns("工作日加班累计");
        doubleColumnSpe.addColumns("休息日加班累计");
        doubleColumnSpe.addColumns("节假日加班累计");
        doubleColumnSpe.addColumns("病假时长");
        doubleColumnSpe.addColumns("事假时长");
        doubleColumnSpe.addColumns("产假时长");
        doubleColumnSpe.addColumns("年假时长");
        doubleColumnSpe.addColumns("调休时长");
        doubleColumnSpe.addColumns("工时调整");
        doubleColumnSpe.addColumns("节假日加班小时-综合");
        doubleColumnSpe.addColumns("月工作日工时-综合");
        doubleColumnSpe.addColumns("季工作日工时-综合");
        doubleColumnSpe.addColumns("年工作日工时-综合");
        doubleColumnSpe.addColumns("迟到次数");
        doubleColumnSpe.addColumns("早退次数");
        doubleColumnSpe.addColumns("签到遗漏次数");
        doubleColumnSpe.addColumns("签退遗漏次数");
        doubleColumnSpe.addColumns("累计迟到分钟数");
        doubleColumnSpe.addColumns("累计早退分钟数");
        doubleColumnSpe.addColumns("缺勤次数");


        columnSpecifications.add(doubleColumnSpe);
        columnSpecifications.add(strNullAbleColumnSpe);
        columnSpecifications.add(doubleNullAbleColumnSpe);
        spreadSheetSpecification.setColumnSpecifications(columnSpecifications);
        return spreadSheetSpecification;
    }

}
